#include <iostream>
#include <string>
#include <stack>
#include <map>
#include <stdio.h>
#include <string.h>
class Solution
{
public:
    bool isValid(string s)
    {
        std::stack<char> stack;
        // const char *ptr = s.c_str(s);
        for (int idx = 0; idx < strlen(s); idx++)
        {
            if (s[idx] == '(' || s[idx] == '{' || s[idx] == '[')
            {
                stack.push(s[idx]);
            }
            else
            {
                if(stack.empty()){
                    return false;
                }
                if (s[idx] == ')' && stack.top() != '(')
                    return false;
                else if (s[idx] == '}' && stack.top() != '{')
                {
                    return false;
                }
                else if (s[idx] == ']' && stack.top() != '[')
                {
                    return false;
                }
                else
                {
                    stack.pop();
                }
            }
        }
        return stack.empty();
    }
}
